﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PMS.Logic.FM
{
    public partial class ReceivePayAjax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String methodName = Request["method"];
            Type type = this.GetType();
            MethodInfo method = type.GetMethod(methodName);
            if (method == null) throw new Exception("method is null");
            try
            {

                method.Invoke(this, null);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 获取所有回款信息
        /// </summary>
        public void GetReceivePayList()
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
                if (listSearch.Where(s => s.Field == "startSignDate").FirstOrDefault() != null)
                {
                    listSearch.Where(s => s.Field == "startSignDate").FirstOrDefault().Field = "Date";
                }
                if (listSearch.Where(s => s.Field == "endSignDate").FirstOrDefault() != null)
                {
                    listSearch.Where(s => s.Field == "endSignDate").FirstOrDefault().Field = "Date";
                }
            }
            Expression<Func<View_FM_ReceivePayList, bool>> exp = SearchExpression.FindByGroup<View_FM_ReceivePayList>(listSearch);
            string OrderId = OperateContext.Sort == null ? "ID" : OperateContext.Sort;
            Boolean OrderSort = OperateContext.Order == true ? OperateContext.Order : false;
            if (OperateContext.Sort == null)
            {
                OrderSort = false;
            }
            int count = 0;
            var list = DALSession.Current.View_FM_ReceivePayListDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, OrderSort);
            count = DALSession.Current.View_FM_ReceivePayListDAL.GetCount(exp);
            Response.Write(JsonHelper.toPageJson(list, count));
        }

        /// <summary>
        /// 根据回款ID获取回款信息
        /// </summary>
        public void GetReceivePayById()
        {
            if (Request.Form["Id"] == null)
            {
                Response.Write(JsonHelper.toDefaultMsg(false, "获取参数Id失败"));
            }
            int Id = Convert.ToInt32(Request.Form["Id"]);
            Response.Write(JsonHelper.toJson(DALSession.Current.View_FM_ReceivePayListDAL.GetListBy(i => i.ID == Id).FirstOrDefault()));
        }

        /// <summary>
        /// 修改回款信息
        /// </summary>
        public void EditReceivePay()
        {
            int ID = Commons.String2Int32(Request.Form["ID"]);
            FM_ReceivePay perNew = DALSession.Current.FM_ReceivePayDAL.GetListBy(d => d.ID == ID).FirstOrDefault();
            ModelTools.Form2EditModel(Request.Form, perNew);
            DALSession.Current.FM_ReceivePayDAL.ModifyAllModel(perNew);
            DALSession.Current.FM_ReceivePayDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }

        /// <summary>
        /// 添加回款信息
        /// </summary>
        public void AddReceivePay()
        {
            FM_ReceivePay per = new FM_ReceivePay();
            ModelTools.Form2EditModel(Request.Form, per);
            per.State = "0";
            DALSession.Current.FM_ReceivePayDAL.Add(per);
            DALSession.Current.FM_ReceivePayDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }

        /// <summary>
        /// 删除回款信息
        /// </summary>
        public void DelReceivePay()
        {
            int Id = Convert.ToInt32(Request.Form["Id"].ToString());
            DALSession.Current.FM_ReceivePayDAL.DelBy(p => p.ID == Id);
            DALSession.Current.FM_ReceivePayDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "删除成功"));
        }

        /// <summary>
        /// 获取所有的回款信息，并按ID倒序排列
        /// </summary>
        public void GetAllReceivePay() {
            Response.Write(JsonHelper.toJson(DALSession.Current.View_FM_ReceivePayListDAL.GetListBy(r =>r.State!="2").OrderByDescending(r=>r.ID)));
        }

        /// <summary>
        /// 校验回款实体是否符合要求
        /// </summary>
        /// <param name="rP"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public Boolean checkReceivePay(FM_ReceivePay rP,String msg) {
            Boolean res = true;
            if (String.IsNullOrEmpty(rP.PayNo)) {
                res = false;
                msg = "回款流水号不能为空！";
            }else if (String.IsNullOrEmpty(rP.Money.ToString())){
                res = false;
                msg = "回款金额不能为空！";
            }
            return res;
        }

    }
}